import { isAnyGranted } from '@/utils/auth/permission-checker'

function checkPermission(el, binding) {
  const { value } = binding
  if (value && value instanceof Array && value.length > 0) {
    const permissionNames = value
    if (!isAnyGranted(permissionNames)) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error(`need permissions! Like v-permission="['AbpIdentity.User','AbpIdentity.Role']"`)
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding)
  },
  update(el, binding) {
    checkPermission(el, binding)
  }
}
